//-----------------------------------------------------------------------
// <copyright file="AddReceiveLocation.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
//-----------------------------------------------------------------------
namespace Microsoft.Sdc.Tasks.BizTalk2006.ReceivePort
{
    using System;
    using System.Globalization;
    using BizTalk.ExplorerOM;

    /// <summary>
    /// Add ReceiveLocation
    /// </summary>
    public class AddReceiveLocation : BizTalk2004.ReceiveLocation.AddReceiveLocation
    {
        private string application;
        private string description;
        private Fragmentation fragmentMessages;
        private bool isprimary;
        private string sendPipeline;
        private string sendPipelineData;
        private string receivePipelineData;

        /// <summary>
        /// Gets or sets the receive pipeline data.
        /// </summary>
        /// <value>The receive pipeline data.</value>
        public string ReceivePipelineData
        {
            get { return this.receivePipelineData; }
            set { this.receivePipelineData = value; }
        }

        /// <summary>
        /// Gets or sets the send pipeline data.
        /// </summary>
        /// <value>The send pipeline data.</value>
        public string SendPipelineData
        {
            get { return this.sendPipelineData; }
            set { this.sendPipelineData = value; }
        }

        /// <summary>
        /// Gets or sets the send pipeline.
        /// </summary>
        /// <value>The send pipeline.</value>
        public string SendPipeline
        {
            get { return this.sendPipeline; }
            set { this.sendPipeline = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is primary.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is primary; otherwise, <c>false</c>.
        /// </value>
        public bool IsPrimary
        {
            get { return this.isprimary; }
            set { this.isprimary = value; }
        }

        /// <summary>
        /// Gets or sets the Fragmentation
        /// </summary>
        /// <value>The fragment messages.</value>
        public Fragmentation FragmentMessages
        {
            get { return this.fragmentMessages; }
            set { this.fragmentMessages = value; }
        }

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>The description.</value>
        public string Description
        {
            get { return this.description; }
            set { this.description = value; }
        }

        /// <summary>
        /// Gets or sets the application.
        /// </summary>
        /// <value>The application.</value>
        public string Application
        {
            get { return this.application; }
            set { this.application = value; }
        }

        /// <summary>
        /// Executes the task
        /// </summary>
        protected override void InternalExecute()
        {
            if (string.IsNullOrEmpty(this.Application))
            {
                base.InternalExecute();
            }
            else
            {
                BtsCatalogExplorer explorer = this.GetBtsCatalogExplorer(this.Server, this.Database);

                Application app = explorer.Applications[this.Application];
                if (app != null)
                {
                    ReceivePort port = app.ReceivePorts[this.ReceivePort];
                    if (this.ReceiveHandler != null && (this.TransportType == null || this.TransportTypeData == null))
                    {
                        Log.LogError("If the ReceiveHandler is specified, the TransportType and TransportTypeData must also be specified");
                        return;
                    }

                    if (port != null)
                    {
                        ReceiveLocation receiveLocation = port.AddNewReceiveLocation();

                        // setting the properties 
                        receiveLocation.Name = this.Name;
                        receiveLocation.Description = this.Description;
                        receiveLocation.Address = this.Address;
                        if (!string.IsNullOrEmpty(this.PublicAddress))
                        {
                            receiveLocation.PublicAddress = this.PublicAddress;
                        }

                        receiveLocation.Enable = this.Enable;
                        receiveLocation.FragmentMessages = this.FragmentMessages;

                        foreach (ReceiveHandler handler in explorer.ReceiveHandlers)
                        {
                            if (handler.Name == this.ReceiveHandler && handler.TransportType.Name == this.TransportType)
                            {
                                receiveLocation.ReceiveHandler = handler;
                                break;
                            }
                        }

                        if (!string.IsNullOrEmpty(this.ReceivePipeline))
                        {
                            string pipelineName;
                            string assemblyName;
                            SplitQualifiedTypeName(this.ReceivePipeline, out pipelineName, out assemblyName);

                            receiveLocation.ReceivePipeline = explorer.Pipelines[pipelineName, assemblyName];
                        }

                        if (!string.IsNullOrEmpty(this.ReceivePipelineData))
                        {
                            receiveLocation.ReceivePipelineData = this.ReceivePipelineData;
                        }

                        if (!string.IsNullOrEmpty(this.SendPipeline) && port.IsTwoWay)
                        {
                            string pipelineName;
                            string assemblyName;
                            SplitQualifiedTypeName(this.SendPipeline, out pipelineName, out assemblyName);

                            receiveLocation.SendPipeline = explorer.Pipelines[pipelineName, assemblyName];
                        }

                        if (!string.IsNullOrEmpty(this.SendPipelineData))
                        {
                            receiveLocation.SendPipelineData = this.SendPipelineData;
                        }

                        receiveLocation.StartDateEnabled = this.StartDateEnabled;
                        receiveLocation.StartDate = (!string.IsNullOrEmpty(this.StartDate)) ? DateTime.Parse(this.StartDate) : receiveLocation.StartDate;

                        receiveLocation.EndDateEnabled = this.EndDateEnabled;
                        receiveLocation.EndDate = (!string.IsNullOrEmpty(this.EndDate)) ? DateTime.Parse(this.EndDate) : receiveLocation.EndDate;

                        receiveLocation.TransportType = explorer.ProtocolTypes[this.TransportType];
                        receiveLocation.TransportTypeData = this.TransportTypeData;

                        receiveLocation.ServiceWindowEnabled = this.ServiceWindowEnabled;
                        Log.LogMessage(string.Format(CultureInfo.InvariantCulture, "Adding RecieveLocation: {0} to RecievePort: {1} in Application {2}", this.Name, this.ReceivePort, this.Application));
                        explorer.SaveChanges();
                    }
                    else
                    {
                        Log.LogError(string.Format(CultureInfo.InvariantCulture, "ReceivePort not found: {0} for Application: {1}", this.ReceivePort, this.Application));
                        return;
                    }
                }
                else
                {
                    Log.LogError(string.Format(CultureInfo.InvariantCulture, "Application not found: {0}", this.Application));
                    return;
                }
            }
        }
    }
}
